﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iMKelly {
    using CoreClass;
    public partial class frmUserManagement : Form {
        Database dbs;
        public frmUserManagement(Database dbs) {
            InitializeComponent();
            this.dbs = dbs;
            RefreshViewer();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }

        private void cmdCreate_Click(object sender, EventArgs e) {
            frmCreateUser frm = new frmCreateUser(dbs);
            frm.MdiParent = this.MdiParent;
            frm.Text = "Create A New User";
            frm.Complete += new CompleHandler(frm_Complete);
            frm.Show();
        }

        void frm_Complete() {
            RefreshViewer();
        }

        private void cmdModify_Click(object sender, EventArgs e) {
            DataGridViewRow row = viewer.CurrentRow;
            UserInfo info = new UserInfo(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[3].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[4].Value.ToString(), row.Cells[5].Value.ToString()=="Male"?true:false, 7);
            frmCreateUser frm = new frmCreateUser(dbs, info);
            frm.MdiParent = this.MdiParent;
            frm.Complete += new CompleHandler(frm_Complete);
            frm.Text = "Modify " + info.UserID;
            frm.Show();
        }

        private void cmdRemove_Click(object sender, EventArgs e) {
            DataGridViewRow row = viewer.CurrentRow;
            UserInfo info = new UserInfo(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[3].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[4].Value.ToString(), row.Cells[5].Value.ToString()=="Male"?true:false, 7);
            if (MessageBox.Show("Do you really want to wipe it out?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == global::System.Windows.Forms.DialogResult.Yes) {
                dbs.removeUser(info.UserID);
            }
        }

        private void RefreshViewer() {
            viewer.Rows.Clear();
            UserInfo[] lst = dbs.getAllUser();
            foreach (UserInfo i in lst) {
                viewer.Rows.Add(new String[] { i.UserID, i.Name, i.Address, i.SSN, i.BirthDate, i.Sex == true?"Male":"Female", i.Privilege.ToString()});
            }
        }
    }
}
